package com.glhtgyxy.biyesheji.Controller;

import com.glhtgyxy.biyesheji.Pojo.merchant;
import com.glhtgyxy.biyesheji.Pojo.user;
import com.glhtgyxy.biyesheji.Service.merchantService;
import com.glhtgyxy.biyesheji.Util.qingkonglist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;

@Controller
@RequestMapping("merchant")
@CrossOrigin(origins = "*")
public class merchantController {
    public static ArrayList<String> imgname = new ArrayList<>();
    @Autowired
    merchantService service;

    @ResponseBody
    @RequestMapping("findallmerchant")
    public ArrayList<merchant> findAllMerchant(){
        System.out.println("所有商户查询中..");
        ArrayList<merchant> merchants = service.findAllMerchant();
        System.out.println("查询完毕，返回:"+merchants);
        return merchants;
    }

    @ResponseBody
    @RequestMapping("merchantaddhouse/{housename}/{addr}/{price}/{bedroom}/{facilityservices}/{masterid}")
    public boolean merchantaddhouse(
            @PathVariable String housename,
            @PathVariable String addr,
            @PathVariable String price,
            @PathVariable String bedroom,
            @PathVariable String facilityservices,
            @PathVariable String masterid
    ){
        System.out.println("添加房屋中..");
        boolean flg  = service.merchantaddhouse(housename,addr,price,bedroom,facilityservices,imgname,masterid);
        qingkonglist.qingkonglist(imgname);
        System.out.println("添加完毕，返回:"+flg);
        return flg;
    }

    @ResponseBody
    @RequestMapping("updatemerchant/{id}/{name}/{account}/{password}")
    public Boolean updateMerchant(@PathVariable int id,
                                  @PathVariable String name,
                                  @PathVariable String account,
                                  @PathVariable String password){
        System.out.println("修改商户中..");
        Boolean merchants = service.updateMerchant(id,name,account,password);
        System.out.println("修改完毕，返回:"+merchants);
        return merchants;
    }
    @ResponseBody
    @RequestMapping("addmerchant/{name}/{account}/{password}")
    public Boolean addMerchant(@PathVariable String name,
                               @PathVariable String account,
                               @PathVariable String password){
        System.out.println("新增商户中..");
        Boolean merchants = service.addMerchant(name,account,password);
        System.out.println("新增完毕，返回:"+merchants);
        return merchants;
    }

    @RequestMapping("deletemerchant/{id}")
    @ResponseBody
    public Boolean deleteMerchant(@PathVariable int id){
        System.out.println("开始删除商户"+id);
        Boolean users=true;
        users= service.deleteMerchant(id);
        System.out.println("执行成功，返回："+ users);
        return users;
    }

    @RequestMapping("shanchufangyuan/{houseid}")
    @ResponseBody
    public Boolean shanchufangyuan(@PathVariable int houseid){
        System.out.println("开始删除房屋"+houseid);
        Boolean flg=true;
        flg= service.shanchufangyuan(houseid);
        System.out.println("执行成功，返回："+ flg);
        return flg;
    }

    @RequestMapping("findmerchant/{serchtext}")
    @ResponseBody
    public ArrayList<merchant> findMerchant(@PathVariable String serchtext){
        System.out.println("开始模糊查找商户"+serchtext);
        ArrayList<merchant> users = service.findMerchant(serchtext);
        System.out.println("执行成功，返回："+ users);
        return users;
    }

    @RequestMapping("quxiao")
    @ResponseBody
    public Boolean quxiao(){
        System.out.println("取消添加房屋");
        boolean flg = true;
        qingkonglist.qingkonglist(imgname);
        flg = CollectionUtils.isEmpty(imgname)?true:false;
        System.out.println(imgname);
        System.out.println("执行成功，返回："+ flg);
        return flg;
    }

}
